package com.xinqi.modules.exam.paper.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.exam.paper.convert.CourseExamPaperStatsConvert;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperStatsEntity;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperStatsCreateDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperStatsDeleteDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperStatsQueryDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperStatsUpdateDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperStatsResultDTO;
import com.xinqi.modules.exam.paper.manager.CourseExamPaperStatsManager;
import com.xinqi.modules.exam.paper.service.CourseExamPaperStatsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 课程测评统计服务层实现
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Validated
@Service("courseExamPaperStatsService")
@RequiredArgsConstructor
public class CourseExamPaperStatsServiceImpl implements CourseExamPaperStatsService {
    private final CourseExamPaperStatsConvert courseExamPaperStatsConvert;
    private final CourseExamPaperStatsManager courseExamPaperStatsManager;

    /**
     * 分页查询 课程测评统计数据
     */
    @Override
    @DS("slaver")
    public PageInfo<CourseExamPaperStatsResultDTO> page(Param pageable, CourseExamPaperStatsQueryDTO query) {
        IPage<CourseExamPaperStatsEntity> page = courseExamPaperStatsManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, courseExamPaperStatsConvert::convert);
    }

    /**
     * 查询 课程测评统计数据
     */
    @Override
    public List<CourseExamPaperStatsResultDTO> find(CourseExamPaperStatsQueryDTO query) {
        return courseExamPaperStatsManager.list(wrapper(query)).stream()
            .map(courseExamPaperStatsConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<CourseExamPaperStatsEntity> wrapper(CourseExamPaperStatsQueryDTO query) {
        LambdaQueryWrapper<CourseExamPaperStatsEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(CourseExamPaperStatsEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 课程测评统计数据
     */
    @Override
    public CourseExamPaperStatsResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(courseExamPaperStatsManager.findById(id)).map(courseExamPaperStatsConvert::convert).orElse(null);
    }

    /**
     * 新增 课程测评统计数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid CourseExamPaperStatsCreateDTO dto) {
        CourseExamPaperStatsEntity entity = courseExamPaperStatsConvert.create(dto);
        courseExamPaperStatsManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 课程测评统计数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid CourseExamPaperStatsUpdateDTO dto) {
        Optional.ofNullable(courseExamPaperStatsManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        CourseExamPaperStatsEntity entity = courseExamPaperStatsConvert.update(dto);
        return courseExamPaperStatsManager.updateById(entity);
    }

    /**
     * 根据id删除 课程测评统计数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return courseExamPaperStatsManager.removeById(id);
    }

    /**
     * 根据id删除 课程测评统计数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(CourseExamPaperStatsDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
